﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace FezPosterClient
{
    class Util
    {
        /// <summary>
        /// Webクライアントの取得
        /// </summary>
        /// <returns></returns>
        public static WebClientEx getWebClient()
        {
            WebClientEx client = new WebClientEx();

            if (Constant.proxyUrl.Length > 0)
            {
                WebProxy proxy = new WebProxy(Constant.proxyUrl);
                client.Proxy = proxy;
            }

            client.Headers["Host"] = Constant.postarDomain;

            return client;
        }

        /// <summary>
        /// Webクライアントの取得
        /// </summary>
        /// <param name="session_id"></param>
        /// <returns></returns>
        public static WebClientEx getWebClient(string session_id)
        {
            WebClientEx client = getWebClient();

            client.CookieContainer = new CookieContainer();
            Cookie ck = new Cookie(Constant.sessionName, session_id, "/", Constant.postarDomain);
            client.CookieContainer.Add(ck);

            return client;
        }

        /// <summary>
        /// アクセス制限がかかっている場合にウェイトをかける
        /// </summary>
        public static void wait()
        {
            if (!Constant.isLimitedAccess)
            {
                return;
            }

            for (int i = 0; i < Constant.limitMicroSecond; i+=100)
            {
                System.Threading.Thread.Sleep(100);
                System.Windows.Forms.Application.DoEvents();
            }
            
        }
    }
}
